package com.minyue.sqlsession;

import com.minyue.pojo.Configuration;
import com.minyue.pojo.MappedStatement;

import java.lang.reflect.*;
import java.util.List;

/**
 * @author ：minyue
 * @date ：Created in 2021/7/22 11:50 下午
 * @description：
 */
public class DefaultSqlSession implements SqlSession {

    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    private Executor simpleExecutor = new SimpleExecutor();

    @Override
    public <E> List<E> selectList(String statementId, Object... param) throws Exception {
        //创建一个处理器对象
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        List<E> list = simpleExecutor.query(configuration, mappedStatement, param);
        return list;
    }

    @Override
    public <T> T selectOne(String statementId, Object... param) throws Exception {
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        List<T> select = simpleExecutor.query(configuration, mappedStatement, param);
        if (select == null) {
            return null;
        }
        return select.get(0);
    }

    @Override
    public int insert(String statementId, Object... param) throws Exception {
        return update(statementId, param);
    }

    @Override
    public int update(String statementId, Object... param) throws Exception {
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        return simpleExecutor.update(configuration, mappedStatement, param);
    }

    @Override
    public int delete(String statementId, Object... param) throws Exception {
        return update(statementId, param);
    }

    @Override
    public <T> T getMapper(Class<T> mapperClass) {
        T o = (T) Proxy.newProxyInstance(mapperClass.getClassLoader(), new Class[]{mapperClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                //通过statement.id去定位选择哪个方法,这里开始进行拼接
                String methodName = method.getName();
                String className = method.getDeclaringClass().getName();
                //进行拼接statementId
                String key = className + "." + methodName;
                Type genericReturnType = method.getGenericReturnType();
                //获取mappedStatement对象，主要目的是为了获取里面的方法类型
                MappedStatement mappedStatement = configuration.getMappedStatementMap().get(key);
                //判断是否实现泛型类型参数化
                switch (mappedStatement.getMethodEnum()) {
                    case INSERT:
                        return insert(key, args);
                    case UPDATE:
                        return update(key, args);
                    case DELETE:
                        return delete(key, args);
                    case SELECT:
                        if (genericReturnType instanceof ParameterizedType) {
                            return selectList(key, args);
                        }
                        return selectOne(key, args);
                    default:
                        throw new RuntimeException("不支持的方法！请出入支持的方法，insert、update、delete、select");
                }

            }
        });
        return o;
    }

    @Override
    public void close() throws Exception {
        simpleExecutor.close();
    }
}
